package cn.shiwei.pay.controller;

import cn.shiwei.basic.exception.MyException;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.order.domain.OrderPet;
import cn.shiwei.pay.domain.PayBill;
import cn.shiwei.pay.dto.AlipayNotifyDto;
import cn.shiwei.pay.service.IPaySerivce;
import cn.shiwei.pet.domain.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IPaySerivce paySerivce;

    @PostMapping("/aliPay/{sn}")
    public RequestStatus aliPay(@PathVariable String sn){
        try {
            String htmlBody = paySerivce.toPay(sn);
            return RequestStatus.me().setResultObj(htmlBody);
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @GetMapping("returnPage")
    public RequestStatus successPage(@RequestParam("sn") String sn){
        try {
            System.out.println("订单号" + sn);
            PayBill payBill = paySerivce.successPage(sn);
            return RequestStatus.me().setResultObj(payBill);
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @PostMapping("/notify")
    public String AlipayNotify(AlipayNotifyDto payNotifyDto){
        System.out.println("接口调用");
        String s = paySerivce.alipayNotify(payNotifyDto);
        System.out.println("向支付宝返回" + s);
        return s;
    }

}
